﻿using Engine;
using Engine.Graphics;
using System.IO;
namespace SCPAK2
{
    public class ModelScreen : Screen
    {
        public int fps = 0;
        public CanvasWidget Canvas = new CanvasWidget() { Size = new Vector2(float.PositiveInfinity), HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
        public FontTextWidget FontText = new FontTextWidget() { Color = Color.Yellow, HorizontalAlignment = WidgetAlignment.Center, FontScale = 3f, Margin = new Vector2(0f, 20f) };
        private string ModelPath;
        public ModelScreen()
        {
        }
        public void ClearAll(ContainerWidget widget) {
            foreach (Widget widget1 in widget.Children) {
                if(widget1 is ContainerWidget)ClearAll((ContainerWidget)widget1);
            }
            widget.Children.Clear();
        }

        public void Init() {
            try {
                if (ModelPath != ModelViewActivity.ModelPath)
                {
                    ClearAll(this);
                    ModelWidget modelWidget = new ModelWidget() { HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center, Size = new Vector2(Display.Viewport.Width) };
                    using (Stream stream = File.OpenRead(ModelViewActivity.ModelPath))
                    {
                        modelWidget.Model = Model.Load(stream, true);
                    }
                    if (File.Exists(ModelViewActivity.TexturePath))
                    {
                        using (Stream stream = File.OpenRead(ModelViewActivity.TexturePath))
                        {
                            modelWidget.TextureOverride = Texture2D.Load(stream, true);
                        }
                    }
                    else
                    {
                        using (Stream stream = ModelViewActivity.Context.Assets.Open("model.png"))
                        {
                            modelWidget.TextureOverride = Texture2D.Load(stream, true);
                        }
                    }
                    SetupBestiaryModelWidget(modelWidget, new Vector3(-1f, 0f, -1f), true, true);
                    Children.Add(Canvas);
                    Canvas.Children.Add(FontText);
                    Canvas.Children.Add(modelWidget);
                    ModelPath = ModelViewActivity.ModelPath;
                }

            }catch {
                ModelViewActivity.Context.Tip("读取模型失败");
            }
        }
        public void SetupBestiaryModelWidget(ModelWidget modelWidget, Vector3 offset, bool autoRotate, bool autoAspect)
        {
            var absoluteTransforms = new Matrix[modelWidget.Model.Bones.Count];
            modelWidget.Model.CopyAbsoluteBoneTransformsTo(absoluteTransforms);
            BoundingBox boundingBox = modelWidget.Model.CalculateAbsoluteBoundingBox(absoluteTransforms);
            float x = MathUtils.Max(boundingBox.Size().X, 1.4f * boundingBox.Size().Y, boundingBox.Size().Z);
            modelWidget.ViewPosition = new Vector3(boundingBox.Center().X, 1.5f, boundingBox.Center().Z) + 2.6f * MathUtils.Pow(x, 0.75f) * offset;
            modelWidget.ViewTarget = boundingBox.Center();
            modelWidget.ViewFov = 0.3f;
            modelWidget.AutoRotationVector = (autoRotate ? new Vector3(0f, MathUtils.Clamp(1.7f / boundingBox.Size().Length(), 0.25f, 1.4f), 0f) : Vector3.Zero);
            if (autoAspect)
            {
                float num = MathUtils.Clamp(boundingBox.Size().XZ.Length() / boundingBox.Size().Y, 1f, 1.5f);
                modelWidget.Size = new Vector2(modelWidget.Size.Y * num, modelWidget.Size.Y);
            }
        }
        public override void Update()
        {
            Init();
            if (Time.PeriodicEvent(1.0, 0.0)) fps = (int)(1f / Time.FrameDuration);
            FontText.Text = "fps  " + fps;
        }

    }
}